//
//  MYGFindListCell.swift
//  MYGrow
//
//  Created by 小火科技 on 2024/3/12.
//

import UIKit
import QMUIKit
import TZImagePickerController

class MYGFindListCell: UICollectionViewCell {
    // MARK: - Property
    var changeAction: (()->Void)?

    var model: MYGFindListModel? {
        didSet {
            guard let model = model else {
                return
            }
            
            nameLabel.text = model.content
            avatar.sd_setImage(with: URL(string: model.doctor.avatar))
            if model.type == 1 {
                bgImageView.sd_setImage(with: URL(string: model.medias.first ?? "")) {[weak self] image, _, _, _ in
                    guard let image = image, let self = self else {
                        return
                    }
                    let scale = image.size.height/image.size.width
                    let imageSize = CGSize(width: (screenWidth-30)/2, height: (screenWidth-30)/2*scale)
                    let labelSize = model.content.labelSize(size: CGSize(width: imageSize.width-18, height: CGFLOAT_MAX), font: .regular(11))
                    let size = CGSize(width: imageSize.width, height: imageSize.height + labelSize.height + 62)
                    self.bgImageView.snp.remakeConstraints { make in
                        make.left.right.top.equalToSuperview()
                        make.size.equalTo(imageSize)
                    }
                    if model.size != size {
                        model.size = size
                        
                        self.changeAction?()
                    }
                }
            } else {
                bgImageView.videoImageWithvideoURL(URL(string: model.medias.first ?? "")!, atTime: 100) {[weak self] image in
                    guard let image = image, let self = self else {
                        return
                    }
                    let scale = image.size.height/image.size.width
                    let imageSize = CGSize(width: (screenWidth-30)/2, height: (screenWidth-30)/2*scale)
                    let labelSize = model.content.labelSize(size: CGSize(width: imageSize.width-18, height: CGFLOAT_MAX), font: .regular(11))
                    let size = CGSize(width: imageSize.width, height: imageSize.height + labelSize.height + 62)
                    self.bgImageView.snp.remakeConstraints { make in
                        make.left.right.top.equalToSuperview()
                        make.size.equalTo(imageSize)
                    }
                    if model.size != size {
                        model.size = size
                        
                        self.changeAction?()
                    }
                }
            }

            userNameLabel.text = model.doctor.name
            starButton.setTitle("\(model.likes)", for: .normal)
            starButton.isSelected = model.is_like > 0
        }
    }
    // MARK: - init
    override init(frame: CGRect) {
        super.init(frame: frame)
        configUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - UI
    private func configUI() {
        backgroundColor = .clear
        
        contentView.addSubview(bgView)
        bgView.addSubview(bgImageView)
        bgView.addSubview(nameLabel)
        bgView.addSubview(avatar)
        bgView.addSubview(userNameLabel)
        bgView.addSubview(starButton)
        
        bgView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        bgImageView.snp.makeConstraints { make in
            make.left.right.top.equalToSuperview()
        }

        nameLabel.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(9)
            make.top.equalTo(bgImageView.snp.bottom).offset(15)
        }
        
        avatar.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(10)
            make.bottom.equalToSuperview().inset(15)
            make.width.height.equalTo(22)
        }
        
        userNameLabel.snp.makeConstraints { make in
            make.left.equalTo(avatar.snp.right).offset(5)
            make.centerY.equalTo(avatar)
        }
        
        starButton.snp.makeConstraints { make in
            make.right.equalToSuperview().inset(10)
            make.centerY.equalTo(avatar)
        }
        
        self.layer.shadowOpacity = 0.18
        self.layer.shadowRadius = 10
        self.layer.shadowOffset = CGSize(width: 0, height: 2)
        self.layer.shadowColor = UIColor.rgba(50, 51, 94).cgColor
        self.layer.cornerRadius = 10
    }
    
    // MARK: - lazy load
    private lazy var bgView: UIView = {
        let view = UIView()
        view.backgroundColor = .white
        view.layer.masksToBounds = true
        view.layer.cornerRadius = 10
        return view
    }()
    
    private lazy var bgImageView: UIImageView = {
        let imgV = UIImageView()
        return imgV
    }()
    
    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(11)
        label.textColor = .black
        label.numberOfLines = 0
        return label
    }()
    
    private lazy var avatar: UIImageView = {
        let imgV = UIImageView()
        imgV.layer.masksToBounds = true
        imgV.layer.cornerRadius = 11
        return imgV
    }()
    
    private lazy var userNameLabel: UILabel = {
        let label = UILabel()
        label.font = .medium(10)
        label.textColor = .rgba(13, 14, 21, 1)
        return label
    }()
    
    private lazy var starButton: QMUIButton = {
        let btn = QMUIButton()
        btn.setImage(UIImage(named: "Find/greate"), for: .normal)
        btn.setImage(UIImage(named: "Find/greate_sel"), for: .selected)
        btn.spacingBetweenImageAndTitle = 3
        btn.imagePosition = .left
        btn.setTitle("685", for: .normal)
        btn.setTitleColor(.rgba(72, 68, 67, 1), for: .normal)
        btn.titleLabel?.font = .regular(10)
        return btn
    }()
}
